package com.training.food.controller;

import com.alibaba.fastjson.JSON;
import com.training.food.Mapper.RetailMapper;
import com.training.food.Mapper.WSmarketMapper;
import com.training.food.pojo.Retail;
import com.training.food.pojo.User;
import com.training.food.pojo.WSmarket;
import com.training.food.service.RetailService;
import com.training.food.service.WSmarketService;
import com.training.food.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class RetailController {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    WSmarketMapper wSmarketMapper;
    @Resource
    RetailService retailService;
    @Resource
    WSmarketService wsmarketService;
    //增加零售市场信息
    @PostMapping("/food/buy/addRetail")
    public CommonResult AddRetail(HttpServletRequest request, @RequestBody Map<String,Object> req){
        Map<String,String>ws= (Map<String, String>) req.get("WSmarket");
        WSmarket wSmarket= JSON.parseObject(JSON.toJSONString(ws),WSmarket.class);
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        System.out.println(wSmarket);
        Retail retail=new Retail();
        retail.setUsername(user.getUsername());
        retail.setInputTime(new Date());
        retail.setWSmarketID(wSmarket.getWSmarketId());
        retail.setState("0");
        retail.setTypeDescription("*");
        retailService.add(retail);

        WSmarket wSmarket1=wSmarketMapper.select(wSmarket);
        wSmarket1.setNumber(wSmarket1.getNumber()-wSmarket.getNumber());
        if(wSmarket1.getNumber()==0){
            wSmarket1.setState("3");
        }

        wsmarketService.update(wSmarket1);

        return CommonResult.success("增加批发市场信息成功");
    }
    //删除零售市场信息
    @PostMapping("/food/buy/subRetail")
    public CommonResult SubRetail(HttpServletRequest request,@RequestBody Retail retail){
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        retail.setUsername(user.getUsername());
        if(retailService.select(retail)) {
            retailService.sub(retail);
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("信息不存在");

    }
    @Autowired
    RetailMapper retailMapper;
    //查询在售订单
    @PostMapping("/food/buy/selectRetailOrder")
    public CommonResult selectOrder(HttpServletRequest request, @RequestBody Retail retail) throws UnsupportedEncodingException {
        List<Retail> res=retailMapper.getOrder(retail.getState());
        if(res==null){
            return CommonResult.failed("没有查到！");
        }
        return CommonResult.success(res);
    }

    //更改零售市场信息
    @PostMapping("/food/buy/updateRetailInfo")
    public CommonResult updateRetailInfo(HttpServletRequest request,@RequestBody Map<String,Object> reqs){
        Map<String,String>re=(Map<String,String>)reqs.get("Retail");

        Retail retail= JSON.parseObject(JSON.toJSONString(re),Retail.class);
        Retail retail1=retailMapper.select(retail);

        retail.setState(retail1.getState());
        retail.setUsername(retail1.getUsername());
        retail.setNumber(retail1.getNumber());
        retail.setWSmarketID(retail1.getWSmarketID());
        retail.setInputTime(retail1.getInputTime());

        retailService.add(retail);

        return CommonResult.success("成功了");
    }
    //获取所有为空的零售市场信息
    @PostMapping("/food/buy/selectAllRetailNull")
    public CommonResult selectAllRetailNull(HttpServletRequest request) {
        return CommonResult.success(retailService.selectAllNull("*"));

    }

    //更改零售市场交易状态
    @PostMapping("/food/buy/updateRetail")
    public CommonResult updateRetail(HttpServletRequest request,@RequestBody Retail retail){
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        Retail retail1=retailService.selectById(retail);
        retail.setUsername(user.getUsername());
        retail1.setUsername(user.getUsername());
        retail1.setState(retail.getState());
        if(retailService.select(retail1)){
            retailService.update(retail1);
            return CommonResult.success("更改成功");
        }
        return CommonResult.failed("零售信息不存在");
    }
    //查询零售市场信息
    @PostMapping("/food/buy/selectRetail")
    public CommonResult selectRetail(HttpServletRequest request, @RequestBody Retail retail) throws UnsupportedEncodingException {
        String token= (String) request.getAttribute("user");
        //System.out.println("token:"+token);
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        //System.out.println();
        if(user==null){
            CommonResult.failed("用户没有找到");
        }
        List<Retail> lists=retailService.selectByUser(user.getUsername(),retail.getState());
        //System.out.println(lists);
        if (lists.size()>0){
            return CommonResult.success(lists);
        }
        return CommonResult.failed("没有找到");
    }
    //查询所有零售市场信息
    @PostMapping("/food/buy/selectAllRetail")
    public CommonResult selectAllRetail(HttpServletRequest request) throws UnsupportedEncodingException{

        List<Retail> lists=retailService.selectAll();

        return CommonResult.success(lists);

    }
    //模糊查询
    @PostMapping("/food/buy/fuzzy")
    public CommonResult fuzzy(HttpServletRequest request,@RequestBody Map<String,Object> req){
        String message= (String) req.get("message");

        return CommonResult.success(retailService.retails(message));
    }
}
